package com.neuedu.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.models.info.Info;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Configuration
public class Knife4jConfiguration {
    @Value("${server.port}")
    private int port;
    /**
     * 根据@Tag 上的排序，写入x-order
     *
     * @return the global open api customizer
     */
    @Bean
    public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
        return openApi -> {
            if (openApi.getTags()!=null){
                openApi.getTags().forEach(tag -> {
                    Map<String,Object> map=new HashMap<>();
                    map.put("x-order", new Random().nextInt(0,100));
                    tag.setExtensions(map);
                });
            }
            if(openApi.getPaths()!=null){
                openApi.addExtension("x-test123","333");
                openApi.getPaths().addExtension("x-abb",new Random().nextInt(1,100));
            }

        };
    }

    @Bean
    public OpenAPI customOpenAPI() {
        Info info = new Info();
        Contact contact = new Contact();
        contact.setName("wangyu");
        contact.setUrl("http://127.0.0.1:"+port);
        contact.setEmail("zhangjg@gmail.com");
        info.setContact(contact);
        return new OpenAPI()
                .info(info
                        .title("管理员模块接口文档")
                        .version("1.0")
                        .description( "管理员模块")
                        .termsOfService("http://doc.wangyu.com")
                        .license(new License().name("Apache 2.0")
                                .url("http://doc.wangyu.com")));
    }
}
